﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Data.Common;
using Data.Model;
using Data.ViewModel;
using DataAccess.BikeManage;
using Ninject;
using WinForm.Common;
using WinForm.Dialog;
using WinForm.Helper;

namespace WinForm.CarManage
{
    public partial class FrmDispatch : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly ILockArea _lockArea = Kernel.Get<ILockArea>();
        private readonly IControlBox _controlBox = Kernel.Get<IControlBox>();

        public FrmDispatch()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 表格绑定数据
        /// </summary>
        /// <param name="areaId"></param>
        private void SetChartValue(int areaId)
        {
            try
            {
                IList<jsh_controlbox> boxes = _controlBox.GetControlBox(areaId).Obj;
                if (boxes.Count > 0)
                {
                    IList<jsh_controlbox> boxesAsc = boxes.OrderBy(b => b.Bikenum).ToList();
                    IList<jsh_controlbox> boxesDesc = boxes.OrderByDescending(b => b.Bikenum).ToList();

                    try
                    {
                        int serial1 = 0;
                        var patchs = new List<DispatchView>();
                        foreach (var box in boxesAsc)
                        {
                            serial1++;
                            patchs.Add(new DispatchView
                            {
                                Serial = serial1,
                                Address = box.Address,
                                Count = box.Bikenum,
                                ID = box.id

                            });
                        }
                        dgvASC.DataSource = patchs;
                    }
                    catch (Exception)
                    {
                        dgvASC.DataSource = boxesAsc;
                    }

                    try
                    {
                        int serial2 = 0;
                        var patchs = new List<DispatchView>();
                        foreach (var box in boxesDesc)
                        {
                            serial2++;
                            patchs.Add(new DispatchView
                            {
                                Serial = serial2,
                                Address = box.Address,
                                Count = box.Bikenum,
                                ID = box.id
                            });
                        }
                        dgvDESC.DataSource = patchs;
                    }
                    catch (Exception)
                    {
                        dgvDESC.DataSource = boxesDesc;
                    }


                }
            }
            catch (Exception)
            {

            }
        }

        private void FrmDispatch_Load(object sender, EventArgs e)
        {
            try
            {
                comboBoxEx1.DataSource = _lockArea.GetLockArea(0).Obj;
                comboBoxEx1.DisplayMember = "Area";
                comboBoxEx1.ValueMember = "id";


                ////图形设置
                int areaid = Converts.ConvertToInt(comboBoxEx1.SelectedValue == null ? "" : comboBoxEx1.SelectedValue.ToString());
                SetChartValue(areaid);
            }
            catch (Exception)
            {

            }

        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            try
            {
                int areaid = Converts.ConvertToInt(comboBoxEx1.SelectedValue == null ? "" : comboBoxEx1.SelectedValue.ToString());
                SetChartValue(areaid);
            }
            catch (Exception)
            {

            }
        }

        private void dgvDESC_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.dgvDESC.SelectedRows == null)
            {
                return;
            }
            //将选中行移向目标表中
            dgvDESC.DoDragDrop(dgvDESC.SelectedRows[0], DragDropEffects.Move);
        }

        /// <summary>
        /// 根据鼠标位置返回dgv的行索引与列索引
        /// </summary>
        /// <param name="nX">当前X坐标</param>
        /// <param name="nY">当前Y坐标</param>
        /// <param name="dgv">DataGridView控件</param>
        /// <returns></returns>
        public Point GetPointInf(int nX, int nY, DataGridView dgv)
        {
            int x = -1;
            int y = -1;
            for (int nR = 0; nR < dgv.Rows.Count; nR++)
            {
                Rectangle rectangle = dgv.GetRowDisplayRectangle(nR, false);
                if (dgv.RectangleToScreen(rectangle).Contains(nX, nY) == true)
                {
                    x = nR;
                }
            }
            for (int nC = 0; nC < dgv.Columns.Count; nC++)
            {
                Rectangle rec = dgv.GetColumnDisplayRectangle(nC, false);
                if (dgv.RectangleToScreen(rec).Contains(nX, nY) == true)
                {
                    y = nC;
                }
            }
            return new Point(x, y);
        }

        /// <summary>
        /// 放开鼠标，拖放完毕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvASC_DragDrop(object sender, DragEventArgs e)
        {
            //dgvDESC选择行
            Point p = new Point();
            p = GetPointInf(e.X, e.Y, dgvASC);
            this.dgvASC.Rows[p.X].Selected = true;

            //弹出调度窗体
            DataGridViewRow fromRow = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
            DataGridViewRow toRow = this.dgvASC.Rows[p.X];
            DispatchView fromdispatch = new DispatchView();
            DispatchView toDispatch = new DispatchView();
            fromdispatch = (DispatchView)fromRow.DataBoundItem;
            toDispatch = (DispatchView)toRow.DataBoundItem;

            EditDispatch ed = new EditDispatch(fromdispatch, toDispatch);
            ed.StartPosition = FormStartPosition.CenterParent;
            ed.ShowDialog();
            if (ed.DialogResult == DialogResult.OK)
            {
                SetChartValue((int)this.comboBoxEx1.SelectedValue);
            }
        }

        /// <summary>
        /// 拖拽进入表格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvASC_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void dgvASC_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }


    }
}
